package demo;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Sky
 * @Date 2017年4月19日 下午3:36:23
 *
 * @version v1.0
 *
 */
public class Test {

    public static void main(String[] args) {
        final Map<String,String> map = new HashMap<String,String>();
        final GuardedByTest next = new GuardedByTest();
        for(int i = 0 ; i < 100 ; i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String value = next.getNext()+"";
                    System.out.println("线程1："+value);
                    map.put(value, value);
                }
            }).start();
        }

        for(int i = 0 ; i < 200 ; i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String value = next.getNext()+"";
                    System.out.println("线程2："+value);
                    map.put(value, value);
                }
            }).start();
        }

        try {
            Thread.sleep(1000);
            System.out.println(map.size());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

